﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeAlgos
{
    public class Primer
    {
        public int EndPoint { get; private set; }


        public Primer (int endPoint)
        {
            EndPoint = endPoint;

            if (endPoint <= 2)
                throw new Exception("EndPont <= 2, choose interger value higher than 2...");
        }


        public int[] GetPrimesBruteForce()
        {
            int counter = 0;
            int[] primes = new int[EndPoint];

            for (int i = 3; i < EndPoint; i++)
            {
                if (i == 1 || i == 2 || i == 3) { primes[++counter] = i; }
                if (i > 3)
                {
                    if (IsPrime(i))
                        primes[++counter] = i;
                }
            }

            return primes;
        }


        public bool IsPrime(int number)
        {
            if (number == 1 || number == 2 || number == 3) { return true; }

            if (number % 2 == 0)                      
                return false;            

            //don't need to check past the square root
            int max = (int) Math.Sqrt(number);
            for (int i = 3; i <= max; i += 2)
            {
                if ((number % i) == 0)
                {
                    return false;
                }
            }
            return true;
        }   
    }
}